﻿namespace WMS6.BLL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.QueryEntity;
    using WMS6.DALFactory;

    public class TaskRuleDetailBLL : IBaseInfoDetail
    {
        private static readonly ICommonMethodDA commonMethod = DataAccess.CreateCommonMethod();
        private List<string> dataBaseID;
        private PubParasInfo pubParasInfo;
        private static readonly ITaskRuleDetailDA taskDetailRuleDA = DataAccess.CreateTaskRuleDetailDA();

        public TaskRuleDetailBLL()
        {
            this.pubParasInfo = null;
        }

        public TaskRuleDetailBLL(string currentWH)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(currentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(currentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001");
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002");
            }
        }

        public TaskRuleDetailBLL(PubParasInfo tempPubParasInfo)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(tempPubParasInfo.CurrentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(tempPubParasInfo.CurrentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001", this.pubParasInfo.UserLanguage);
                }
                else
                {
                    this.pubParasInfo = new PubParasInfo();
                    this.pubParasInfo = tempPubParasInfo;
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002", this.pubParasInfo.UserLanguage);
            }
        }

        public string DeleteDetail(List<string> taskRuleID, List<string> lineID, List<string> pk3)
        {
            string temp = string.Empty;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                int i;
                if (taskRuleID.Count != lineID.Count)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("F6050_005", this.pubParasInfo.UserLanguage);
                }
                List<TaskRuleDetailInfo> taskRuleDetailInfos = new List<TaskRuleDetailInfo>();
                TaskRuleDetailInfo taskRuleDetailInfo = null;
                for (i = 0; i < taskRuleID.Count; i++)
                {
                    taskRuleDetailInfo = taskDetailRuleDA.GetTaskRuleDetailByID(dataBase, tran, taskRuleID[i], lineID[i], this.dataBaseID[0], this.pubParasInfo);
                    taskRuleDetailInfos.Add(taskRuleDetailInfo);
                }
                AuditDataBLL auditDataBLL = new AuditDataBLL();
                taskDetailRuleDA.DeleteTaskRuleDetail(dataBase, tran, taskRuleID, lineID, this.dataBaseID);
                for (i = 0; i < taskRuleID.Count; i++)
                {
                    auditDataBLL.InsertAuditData<TaskRuleDetailInfo>(dataBase, tran, taskRuleDetailInfos[i], null, AuditAction.Delete, this.pubParasInfo.UserID, "TASK_RULE_DETAIL", this.dataBaseID[0]);
                    string CS00002 = temp;
                    temp = CS00002 + taskRuleID[i] + "," + lineID[i] + ";";
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            if (temp.Length > 0)
            {
                return temp.Substring(0, temp.Length - 1);
            }
            return "";
        }

        public DataSet GetAllDetailInfo()
        {
            return taskDetailRuleDA.GetAllTaskRuleDetail(this.dataBaseID[0], this.pubParasInfo);
        }

        public DataSet GetDetaiInfolByQueryEntity(object obj)
        {
            TaskRuleDetailQueryEntity taskRuleDetail = obj as TaskRuleDetailQueryEntity;
            List<string> sqlWhere = new List<string>();
            if (!string.IsNullOrEmpty(taskRuleDetail.TaskRuleID))
            {
                sqlWhere.Add(" TASK_RULE_ID " + taskRuleDetail.TaskRuleID);
            }
            if (!string.IsNullOrEmpty(taskRuleDetail.LineID))
            {
                sqlWhere.Add(" LINE_ID " + taskRuleDetail.LineID);
            }
            return taskDetailRuleDA.GetTaskRuleDetailByQueryList(sqlWhere, taskRuleDetail, this.dataBaseID[0], this.pubParasInfo);
        }

        public object GetDetailInfoByID(string taskRuleID, string lineID, string pk3)
        {
            return taskDetailRuleDA.GetTaskRuleDetailByID(taskRuleID, lineID, this.dataBaseID[0], this.pubParasInfo);
        }

        public List<object> GetDetailInfoListByID(string taskRuleID, string subID)
        {
            List<TaskRuleDetailInfo> taskRuleDetail = taskDetailRuleDA.GetTaskRuleDetailListByID(taskRuleID, this.dataBaseID[0], this.pubParasInfo);
            List<object> objs = null;
            if ((taskRuleDetail != null) && (taskRuleDetail.Count > 0))
            {
                objs = new List<object>();
                for (int i = 0; i < taskRuleDetail.Count; i++)
                {
                    objs.Add(taskRuleDetail[i]);
                }
            }
            return objs;
        }

        public string InsertDetail(object obj)
        {
            TaskRuleDetailInfo taskRuleDetailInfo = obj as TaskRuleDetailInfo;
            string LineID = string.Empty;
            try
            {
                if (taskDetailRuleDA.CheckTaskRuleDetailIDUnique(taskRuleDetailInfo.TaskRuleID, taskRuleDetailInfo.LineID.ToString(), this.dataBaseID[0]))
                {
                    PublicMethod.GetInstance().ShowAlertMessage("F6050_004", this.pubParasInfo.UserLanguage, new List<string> { taskRuleDetailInfo.TaskRuleID, taskRuleDetailInfo.LineID.ToString() });
                }
                if (taskRuleDetailInfo == null)
                {
                    return LineID;
                }
                taskRuleDetailInfo.UpdatedBy = taskRuleDetailInfo.CreatedBy;
                taskRuleDetailInfo.CreatedDate = DateTime.Now;
                taskRuleDetailInfo.UpdatedDate = DateTime.Now;
                if (taskDetailRuleDA.InsertTaskRuleDetail(taskRuleDetailInfo, this.dataBaseID) > 0)
                {
                    new AuditDataBLL().InsertAuditData<TaskRuleDetailInfo>(null, taskRuleDetailInfo, AuditAction.Insert, this.pubParasInfo.UserID, "TASK_RULE_DETAIL", this.dataBaseID[0]);
                    LineID = taskRuleDetailInfo.LineID.ToString();
                }
            }
            catch (Exception ex)
            {
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            return LineID;
        }

        public string UpdateDetail(object obj)
        {
            string LineID = string.Empty;
            try
            {
                TaskRuleDetailInfo taskRuleDetailInfo = obj as TaskRuleDetailInfo;
                TaskRuleDetailInfo oldTaskRuleDetailInfo = taskDetailRuleDA.GetTaskRuleDetailByID(taskRuleDetailInfo.TaskRuleID, taskRuleDetailInfo.LineID.ToString(), this.dataBaseID[0], this.pubParasInfo);
                if (taskDetailRuleDA.UpdateTaskRuleDetail(taskRuleDetailInfo, this.dataBaseID) > 0)
                {
                    new AuditDataBLL().InsertAuditData<TaskRuleDetailInfo>(oldTaskRuleDetailInfo, taskRuleDetailInfo, AuditAction.Update, this.pubParasInfo.UserID, "TASK_RULE_DETAIL", this.dataBaseID[0]);
                    LineID = taskRuleDetailInfo.TaskRuleID + " , " + taskRuleDetailInfo.LineID;
                }
            }
            catch (Exception ex)
            {
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            return LineID;
        }

        public string UpdateIsActive(List<string> mainID, List<string> subId, string isActive)
        {
            throw new NotImplementedException();
        }
    }
}

